 #define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr) return head;

        ListNode* newhead = new ListNode(0);
        newhead->next = head;
        ListNode* prev = newhead, * cur = newhead->next, * next = cur->next, * nnext = next->next;

        while (cur && next)
        {
            //交换节点
            prev->next = next;
            next->next = cur;
            cur->next = nnext;

            //更换指针
            prev = cur;
            cur = nnext;
            if (cur) next = cur->next;
            if (next) nnext = next->next;
        }

        cur = newhead->next;
        delete newhead;
        return cur;
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        //处理边界情况
        if (head == nullptr || head->next == nullptr || head->next->next == nullptr) return;

        //1.找出中间节点
        ListNode* slow = head, * fast = head;
        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }

        //2.翻转slow后面的节点
        ListNode* head1 = new ListNode(0);
        ListNode* cur = slow->next;
        //断开原本的节点,将两个链表分开
        slow->next = nullptr;
        //采用头插法
        while (cur)
        {
            ListNode* next = cur->next;
            cur->next = head1->next;
            head1->next = cur;
            cur = next;
        }

        //3.合并两个链表
        ListNode* newhead = new ListNode(0);
        ListNode* prev = newhead;
        ListNode* cur1 = head, * cur2 = head1->next;
        while (cur1)
        {
            //先合并第一个链表
            prev->next = cur1;
            cur1 = cur1->next;
            prev = prev->next;

            //合并第二个链表
            if (cur2)
            {
                prev->next = cur2;
                cur2 = cur2->next;
                prev = prev->next;
            }
        }
        delete newhead;
        delete head1;
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    struct cmp
    {
        bool operator()(const ListNode* cur1, const ListNode* cur2)
        {
            return cur1->val > cur2->val;
        }
    };


    ListNode* mergeKLists(vector<ListNode*>& lists) {
        //创建一个小根堆
        priority_queue<ListNode*, vector<ListNode*>, cmp> heap;

        for (auto l : lists)
            if (l) heap.push(l);

        ListNode* newhead = new ListNode(0);
        ListNode* prev = newhead;
        //合并n个链表
        while (!heap.empty())
        {
            //取出堆顶
            ListNode* t = heap.top();
            heap.pop();

            prev->next = t;
            prev = prev->next;
            if (t->next) heap.push(t->next);
        }
        prev = newhead->next;
        delete newhead;
        return prev;
    }
};